/*
 * @Description: 全局mixin
 * @Version: 2.0
 * @Autor: Seven
 * @Date: 2020-12-10 10:43:30
 * @LastEditors: Seven
 * @LastEditTime: 2020-12-17 10:48:26
 */
import Vue from 'vue'

Vue.prototype.IMG_URL = 'http://edu-oss.cstaole.com/'

// 将字符串html转换为html
Vue.directive('exchangeHtml', {
    bind: function (el, foo) {
        el.innerHTML += foo.value
    }
})

/*
** 时间戳转换成指定格式日期
** eg. 
** dateFormat(11111111111111, 'Y年m月d日 H时i分')
** → "2322年02月06日 03时45分"
*/
Vue.prototype.dateFormat = function (timestamp, formats) {
    // formats格式包括
    // 1. Y-m-d
    // 2. Y-m-d H:i:s
    // 3. Y年m月d日
    // 4. Y年m月d日 H时i分
    formats = formats || 'Y-m-d';

    var zero = function (value) {
        if (value < 10) {
            return '0' + value;
        }
        return value;
    };

    var myDate = timestamp ? new Date(timestamp) : new Date();

    var year = myDate.getFullYear();
    var month = zero(myDate.getMonth() + 1);
    var day = zero(myDate.getDate());

    var hour = zero(myDate.getHours());
    var minite = zero(myDate.getMinutes());
    var second = zero(myDate.getSeconds());

    return formats.replace(/Y|m|d|H|i|s/ig, function (matches) {
        return ({
            Y: year,
            m: month,
            d: day,
            H: hour,
            i: minite,
            s: second
        })[matches];
    });
}